﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Model;
using System.Data;
using System.IO;

public partial class NewsManagement_AddNews : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //RadioButtonList1.Enabled = false;
            ////RequiredFieldValidator3.Enabled = false;
            //trc.Visible = false;
            ShowNewHtml();
            NewsCategoryBind(); 
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(this.ddlNewsCategory.SelectedValue) == -1)
        {
            this.lblManage.Text = "请选择分类";
            return;
        }
        if (this.edtrText.Text.Trim() == "")
        {
            this.lblNewsContent.Text = "请填写新闻内容";
            return;
        }
        NewsModule nm = new NewsModule();
        V_News nw = new V_News();
        nw.CreatedTime = DateTime.Now;
        nw.CreaterId = UserID;
        nw.CreaterName = RealName.ToString();
        nw.UpdateTime = DateTime.Now;
        nw.NewsTitle = this.txtNewsTitle.Text;
        nw.NewsContent = this.edtrText.Text;
        nw.NewsAuthor = this.txtAuthor.Text;
        nw.SchoolId = this.CurrentUser.SchoolID;
        nw.NewsImage = string.Empty;
        //if (AdminType == 4)
        //{
        //    nw.IsPublished = 0;
        //}
        //else
        //{
        //    nw.IsPublished = Convert.ToInt32(RadioButtonList1.SelectedValue);
        //}
        nw.IsPublished = Convert.ToInt32(RadioButtonList1.SelectedValue);
        string fileExtension = string.Empty;
        if (!string.IsNullOrEmpty(this.fuNewsImage.FileName))//修改图片
        {
            fileExtension = System.IO.Path.GetExtension(this.fuNewsImage.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
        }    

        nw.CategoryId = Convert.ToInt32(this.ddlNewsCategory.SelectedValue);
        int resurt = nm.AddNews(nw);
        if (resurt > 0)
        {            
            if (!string.IsNullOrEmpty(this.fuNewsImage.FileName))//修改图片
            {
                fileExtension = System.IO.Path.GetExtension(this.fuNewsImage.FileName);
                if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
                {
                    PageHelper.AlterMsg("图片格式只支持jpg/gif");
                    return;
                }
                string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
                dictionarySavedPath += "\\Images\\SchoolId\\News\\";
                dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", this.SchoolID.ToString());
                if (!Directory.Exists(dictionarySavedPath))
                {
                    Directory.CreateDirectory(dictionarySavedPath);
                }
                // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
                //保存logo的文件夹绝对路径

                string logfileSavedPath = dictionarySavedPath +  resurt.ToString() + fileExtension;
                //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
                this.fuNewsImage.SaveAs(logfileSavedPath);

                nw.NewsImage = ("/Images/SchoolId/News/" +  resurt.ToString() + fileExtension).Replace("SchoolId", this.SchoolID.ToString());

                nm.UpdateNews(resurt, nw.NewsImage);
            }
        }
        if (resurt > 0)
        {
            PageHelper.AlterMsgAndRedirect("添加成功！", "ViewNewsList.aspx");
        }
    }

    //查询新闻类别绑定
    private void NewsCategoryBind()
    {
        NewsCategoryModule ncm = new NewsCategoryModule();
        int schoolid = this.CurrentUser.SchoolID;
        DataSet ds = ncm.QueryNewsCategoryByschool(schoolid);
        ddlNewsCategory.DataSource = ds;
        ddlNewsCategory.DataBind();
    }
    private void ShowNewHtml()
    {
        DataSet ds = new DataSet();
        AdminModule am = new AdminModule();
        string typeva = AdminType.ToString();
        ds = am.GetAdmin(-1, typeva);
        DataRow dr = ds.Tables[0].Rows[0];
        int result = Convert.ToInt32(dr["VA_type"]);
        //if (result != 4)
        //{
        //    trc.Visible = true;
        //    RadioButtonList1.Enabled = true;
        //    //RequiredFieldValidator3.Enabled = true;            
        //}
        
    }
}